package com.mty.mydemo.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.RemoteViews;
import com.litesuits.android.log.Log;
import com.mty.mydemo.R;
import com.mty.mydemo.act.home.HomeActivity;
import com.qunduikj.sjb.sdk.consts.AppConst;

import java.util.Random;

/**
 * @ClassName:${MusicService}
 * @Description：音乐播放服务
 * Created by ${mty} on 2016/3/16.
 * http://blog.csdn.net/yunyuliunian
 */
public class MusicService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    String musicName;
    String musicText;

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        switch (intent.getIntExtra(AppConst.MusicType,0)){
            case 0:
                break;
            case AppConst.LastOne:
                Log.e("MusicService","LastOne");
                break;
            case  AppConst.Pause:
                Log.e("MusicService","Pause");
                break;
            case  AppConst.NextOne:
                Log.e("MusicService","NextOne");
                break;
            case  AppConst.Close:
                stopService(new Intent(this,MusicService.class));
                Log.e("MusicService","Close");
                break;
        }

        displayNotification();
        return super.onStartCommand(intent, flags, startId);
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void displayNotification() {
        RemoteViews mRemoteViews= new RemoteViews(getPackageName(), R.layout.view_notification);
        mRemoteViews.setTextViewText(R.id.view_remote_name,"情非得已");
        mRemoteViews.setTextViewText(R.id.view_remote_txt,"发酸发飞洒飞洒发烧发撒打算打算烧发烧大大大大松发烧发烧");
        Intent lastintent=new Intent(getApplicationContext(),MusicService.class);
        Intent pauseintent=new Intent(getApplicationContext(),MusicService.class);
        Intent nextintent=new Intent(getApplicationContext(),MusicService.class);
        Intent closeintent=new Intent(getApplicationContext(),MusicService.class);
        lastintent.putExtra(AppConst.MusicType,AppConst.LastOne);
        pauseintent.putExtra(AppConst.MusicType,AppConst.Pause);
        nextintent.putExtra(AppConst.MusicType,AppConst.NextOne);
        closeintent.putExtra(AppConst.MusicType,AppConst.Close);
        PendingIntent pendinglast = PendingIntent.getService(this, 101, lastintent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingpause = PendingIntent.getService(this, 102, pauseintent,PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingnext = PendingIntent.getService(this, 103, nextintent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingclose = PendingIntent.getService(this, 104, closeintent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.view_remote_last,pendinglast);
        mRemoteViews.setOnClickPendingIntent(R.id.view_remote_pause,pendingpause);
        mRemoteViews.setOnClickPendingIntent(R.id.view_remote_next,pendingnext);
        mRemoteViews.setOnClickPendingIntent(R.id.view_remote_close,pendingclose);
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent piResult0 = PendingIntent.getActivity(MusicService.this,  (new Random().nextInt(100)), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification noti = new Notification.Builder(MusicService.this)
                .setContentIntent(piResult0)
                .setOngoing(true)
                .setContentText("情非得已")
                .setContentTitle("我的播放器")
                .setTicker("音乐播放中") //通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
                .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setSmallIcon(R.drawable.ic_launcher)//设置通知小ICON
                .build();
        noti.bigContentView=mRemoteViews;
        startForeground(3, noti);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }
}
